package com.tc.zigbee.entity;

import java.util.LinkedList;
import java.util.List;

import com.tc.gateway.device.Scene;
import com.tc.util.DataUtil;
import com.tc.zigbee.zcl.command.ClusterMeta;
import com.tc.zigbee.zcl.command.DataType;
import com.tc.zigbee.zcl.command.DataValue;
import com.tc.zigbee.zcl.command.SceneExtensionMeta;
//Extension field sets =
//{{ClusterID 1, length 1, {extension field set 1}}, {ClusterID 2, length 2,
//{extension field set 2 }}, ... }.
//The attributes included in the extension
public class ZigbeeScene extends Scene{
	

	public short getTransitionTime() {
		return transitionTime;
	}
	public void setTransitionTime(short transitionTime) {
		this.transitionTime = transitionTime;
	}
	public byte[] getSceneName() {
		return sceneName;
	}
	public void setSceneName( byte[] sceneName) {
		this.sceneName = sceneName;
	}
	public List<SceneClusterRecord> getClusterRecords() {
		return clusterRecords;
	}
	public void setClusterRecords(List<SceneClusterRecord> clusterRecords) {
		this.clusterRecords = clusterRecords;
	}
	short transitionTime;
	 byte[] sceneName;
	 List<SceneClusterRecord> clusterRecords = new LinkedList<SceneClusterRecord>();
	 byte length(){
		 byte s = 0;
			for(SceneClusterRecord v : clusterRecords ){
				s += v.size();
			}
			return (byte)(s + 5 + sceneName.length + 1);
	 }
	 
	 public byte[] payload(){
			byte[] p = new byte[length()];
			byte[] buf = DataUtil.bytesValue(group);
			int i = 0;
			p[i++] = buf[0];
			p[i++] = buf[1];
			p[i++] = scene;
			buf = DataUtil.bytesValue(this.transitionTime);
			p[i++] = buf[0];
			p[i++] = buf[1];
			p[i++] = (byte)sceneName.length;
			for(byte t : sceneName)
				p[i++] = t;
			for(SceneClusterRecord asc : clusterRecords){
				buf = DataUtil.bytesValue(asc.clusterId);
				p[i++] = buf[0];
				p[i++] = buf[1];
				p[i++] = buf[0];
				p[i++] = asc.valueLength();
				for(DataValue t : asc.attributeValues){
					for(byte b : t.value){
						p[i++] = b;
					}
				}
				
			}
		return p;	
		}
	 public ZigbeeScene(byte[] data){
		 int i = 0;
		 byte status = data[i++];
		
		 byte[] buf = new byte[2];
		 buf[0] = data[i++];
		 buf[0] = data[i++];
		 group = DataUtil.shortValue(buf);
		 scene = data[i++];
		 if(status != 0)
			 return;
		 buf[0] = data[i++];
		 buf[0] = data[i++];
		 this.transitionTime = DataUtil.shortValue(buf);
		 byte len = data[i++];
		 sceneName = new byte[len];
		 for(int j = 0; j < len; j++)
			 sceneName[j] = data[i++];
		 while(i < data.length){
			 SceneClusterRecord r = new SceneClusterRecord();
			 buf[0] = data[i++];
			 buf[0] = data[i++];
			 short cid = DataUtil.shortValue(buf);
			 r.setClusterId(cid);
			 
			 ClusterMeta meta = SceneExtensionMeta.getClusterMeta(cid);
			 byte el = data[i++];
			 int ss = 0;
			 int ai = 0;
			 while(ss < el){
				 
				 DataType dt = meta.getAttributeDataTypes()[ai++];
				 byte[] vb = new byte[dt.getLength()];
				 for(int x = 0; x < dt.getLength(); x++){
					 vb[x] = data[i++];
				 }
				 DataValue v = new DataValue(dt.getLength(), vb);
				 r.getAttributeValues().add(v);
				 ai++;
				 ss += dt.getLength();
				 
			 }
			 clusterRecords.add(r);
			 
		 }
		 
	 }
	public ZigbeeScene() {
		// TODO Auto-generated constructor stub
	}
}
class SceneClusterRecord{
	short clusterId;
	List<DataValue> attributeValues = new LinkedList<DataValue>();
	public short getClusterId() {
		return clusterId;
	}
	public void setClusterId(short clusterId) {
		this.clusterId = clusterId;
	}

	public List<DataValue> getAttributeValues() {
		return attributeValues;
	}
	public void setAttributeValues(List<DataValue> attributeValues) {
		this.attributeValues = attributeValues;
	}
	public byte size(){

		return (byte)( valueLength() + 3);
	}
	byte valueLength(){
		byte s = 0;
		for(DataValue v : attributeValues ){
			s += v.value.length;
		}
		return s;
	}
	
}